<template>
	<div class="news-list">
		<!-- Tab Buttons -->
		<div class="tabs">
			<button @click="switchCategory(0)" :class="{ active: cat === 0 }">官方</button>
			<button @click="switchCategory(1)" :class="{ active: cat === 1 }">民间</button>
		</div>

		<div v-for="(card, index) in cardList" :key="index" class="card" @click="navigateToDetail(card.id)">
			<div class="content-row">
				<image class="image" :src="card.cover"></image>
				<div class="content-container">
					<div class="header">
						<text class="title">{{ card.title }}</text>
					</div>
					<text class="description">{{ card.text }}</text>
					<div class="footer">
						<u-icon name="clock-fill" color="#666" size="14"  ></u-icon>
						<text class="date">日期{{ card.date }}</text>
					</div>
				</div>

			</div>
		</div>
		<div class="p4"></div>
		<van-pagination v-model:current-page="currentPage" :total-items="totalItems" :items-per-page="itemsPerPage"
			@change="fetchStoryList" />
	</div>
</template>

<script>
	import {
		listStory
	} from "@/api/system/story";

	export default {
		data() {
			return {
				cardList: [],
				currentPage: 1,
				totalItems: 0,
				itemsPerPage: 4,
				loading: true,
				cat: 0, // Default category is official
			};
		},
		methods: {
			async fetchStoryList(page = 1) {
				this.loading = true;
				try {
					const response = await listStory({
						pageNum: page,
						pageSize: this.itemsPerPage,
						cat: this.cat, // Include category in API request
					});
					if (response && response.code === 200) {
						const {
							rows,
							total
						} = response;
						this.totalItems = total;
						this.cardList = rows.map((food) => ({
							title: food.title,
							cover: food.cover || "default-image-url",
							text: food.info || "No information available",
							id: food.id,
							date: food.createTime,
						}));
					} else {
						console.error("Failed to fetch Food list:", response.msg || "Unknown error");
					}
				} catch (error) {
					console.error("Failed to fetch Food list:", error);
				} finally {
					this.loading = false;
				}
			},
			navigateToDetail(id) {
				this.$tab.navigateTo(`/pages/story/story?id=${id}`);
			},
			switchCategory(newCat) {
				this.cat = newCat;
				this.currentPage = 1; // Reset to first page
				this.fetchStoryList();
			},
		},
		created() {
			this.fetchStoryList();
		},
	};
</script>

<style scoped>
	.p4 {
		padding-top: 10px;
	}

	.tabs {
		display: flex;
		width: 100%;
		/* 确保容器宽度为100% */
		margin-bottom: 20px;
	}

	.tabs button {
		flex: 1;
		/* 每个按钮占据相等的空间 */
		background: none;
		border: none;
		font-size: 16px;
		padding: 0px 0;
		/* 只设置上下内边距 */
		cursor: pointer;
		transition: color 0.3s ease;
		text-align: center;
		/* 文本水平居中 */
		border-radius: 0;
		/* 移除圆角 */
		box-sizing: border-box;
		/* 确保内边距和边框包含在按钮宽度内 */
	}

	.tabs button.active {
		font-weight: bold;
		color: #007BFF;
		border-bottom: 0px solid #fff;
		background: #fff;
	}


	.card {
		background-color: white;
		position: relative;
		width: 96%;
		max-width: 600px;
		min-height: 120px;
		padding: 0px;
		margin: 5px 0 0px 0px;
		border-radius: 15px;
		overflow: hidden;
		box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
		cursor: pointer;
		border: 1px solid #ccc;
		transition: box-shadow 0.3s ease, transform 0.3s ease;
	}

	.card:hover {
		transform: scale(1.05);
		box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
	}

	.content-row {
		display: flex;
		flex-direction: row;
		width: 90%;
		padding: 5% 5%;
	}

	.image {
		width: 160px;
		height: 100px;
		border-radius: 10px;
		margin-left: 0px;
		margin-right: 10px;
	}

	.content-container {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		width: calc(100% - 136px);
		/* Adjust width to exclude the image and its margin */
	}

	.header {
		margin-bottom: 10px;
	}

	.title {
		font-size: 18px;
		font-weight: bold;
	}

	.description {
		font-size: 14px;
		color: #555;
		margin-bottom: 12px;
	}

	.footer {
		display: flex;
		align-items: center;
	}

	.iconfont {
		font-size: 16px;
		margin-right: 8px;
	}

	.date {
		font-size: 12px;
		color: #888;
	}

	.van-pagination {
		padding-bottom: 20px;
	}

	.news-list {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		margin-left: 0%;
	}
</style>